<!-- 新增/编辑指标 -->
<template>
  <a-drawer
    width="45%"
    :visible="props.visible"
    @ok="handleOk"
    @cancel="handleCancel"
    draggable
    title-align="center"
    unmountOnClose
  >
    <template #title> {{ props.data?.id ? '编辑指标' : '新增指标' }} </template>
    <div>
      <a-form
        :model="formData.data"
        ref="refForm"
        :rules="rules"
        auto-label-width
      >
        <a-form-item label="工作类型" field="type" show-colon>
          <a-select v-model="formData.data.type">
            <a-option :value="1">重点工作</a-option>
            <a-option :value="2">例行工作</a-option>
            <a-option :value="3">新增工作</a-option>
          </a-select>
        </a-form-item>
        <a-form-item label="工作计划项目名称" field="project" show-colon>
          <a-input v-model="formData.data.project"></a-input>
        </a-form-item>
        <a-form-item label="工作项目界定/描述" field="describe" show-colon>
          <a-textarea
            v-model="formData.data.describe"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="完成目标及时限要求" field="require" show-colon>
          <a-textarea
            v-model="formData.data.require"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>

        <a-form-item label="评价标准/规则" field="standard" show-colon>
          <a-textarea
            v-model="formData.data.standard"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="评分上限" field="upper" show-colon>
          <a-tooltip
            content="注：加和计算评分规则下，每一项指标最高评分上限不得超过总分上限，
且各项指标得分和，要小于等于总分上限。
加权计算评分规则下，每一项指标的评分上限默认等于总分上限。"
          >
            <icon-info-circle-fill style="color: #165dff; font-size: 16px" />
          </a-tooltip>
          <a-input-number
            v-model="formData.data.upper"
            hide-button
            :disabled="
              toolStore.getCalculate === 2 && toolStore.geUpperLimit === 1
            "
          >
            <template #suffix>
              <div>分</div>
            </template>
          </a-input-number>
        </a-form-item>
        <a-form-item
          label="指标权重"
          field="weight"
          show-colon
          v-if="toolStore.getCalculate === 2"
          :tooltip="`最大权重不能超过${
            100 -
            props.maxWeight +
            (props?.data?.weight ? props?.data?.weight : 0)
          }`"
        >
          <a-input-number
            v-model="formData.data.weight"
            hide-button
            :min="0.01"
            :precision="2"
            :max="
              100 -
              props.maxWeight +
              (props?.data?.weight ? props?.data?.weight : 0)
            "
          >
            <template #suffix>
              <div>%</div>
            </template>
          </a-input-number>
        </a-form-item>
        <a-form-item label="评分方式" field="way" show-colon>
          <a-radio-group v-model="formData.data.way">
            <a-radio :value="1">直接输入</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="评分人" field="personnel" show-colon>
          <!-- 系统自动评分 -->
          <a-tooltip
            content="注：评分人在平常的情况下，都是默认关闭，只有特殊指定的模板，才
开启，并且一直会指定某个指定的人员进行评分。
平常关闭状态下是以每个单独评价中”的设置定的评分人为准"
          >
            <icon-info-circle-fill style="color: #165dff; font-size: 16px" />
          </a-tooltip>
          <a-switch
            v-model="formData.data.personnel"
            :checked-value="1"
            :unchecked-value="2"
          ></a-switch>
        </a-form-item>
        <a-form-item label="备注" field="remark" show-colon>
          <a-textarea
            v-model="formData.data.remark"
            show-word-limit
            :max-length="50"
            :auto-size="{
              minRows: 4
            }"
          ></a-textarea>
        </a-form-item>
        <a-form-item label="新增到指标库" field="" show-colon>
          <a-radio-group v-model="formData.isAddStore">
            <a-radio :value="true">是</a-radio>
            <a-radio :value="false">否</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
      <!-- <CountSetting v-model:visible="countSettingVisiable"></CountSetting> -->
    </div>
    <template #footer>
      <a-space>
        <a-button @click="handleCancel">取消</a-button>
        <a-button type="primary" @click="handleOk">确认</a-button>
        <a-button v-if="isShowContinueBtn" type="primary" @click="continueAdd"
          >确认并继续新增</a-button
        >
      </a-space>
    </template>
  </a-drawer>
</template>

<script lang="ts" setup>
import { cloneDeep } from 'lodash';
import { useAssessmentToolStore } from '@/store';
import CountSetting from '../CountSetting/index.vue';

const toolStore = useAssessmentToolStore();
const props = withDefaults(
  defineProps<{
    visible: boolean; // 是否显示对话框
    data?: any; // 传进来的数据,
    weight: number; // ppi权重
    maxWeight: number; // ppi所有子数据权重加起来的权重
  }>(),
  {}
);

const emits = defineEmits(['update:visible', 'ppiIndexEmit']);
let refForm = ref();
let countSettingVisiable = ref(false); // 是否设置计算公式弹出框
let isShowContinueBtn = ref(true); // 是否显示继续新增按钮
const formData = reactive<any>({
  data: {
    type: undefined, // 工作类型
    project: '', // 工作计划项目
    describe: '', // 工作项目界定、描述
    require: '', // 完成目标及时限要求
    standard: undefined, // 评分标准
    weight: 0, // 指标权重
    dataSource: undefined, // 评分人（userid）
    personne: 1, // 评分人是否开启 1开 2否
    upper: 0, // 评分上限
    way: 1, // 评分方式 2非手动 1手动
    remark: '', // 备注
    personnel: 2 // 评分人是否开启0否1是
    // dimensionaId: '' // 维度id
  }
});

// 原始数据
const originData = {
  type: undefined, // 工作类型
  project: '', // 工作计划项目
  describe: '', // 工作项目界定、描述
  require: '', // 完成目标及时限要求
  standard: undefined, // 评分标准
  weight: 0, // 指标权重
  dataSource: undefined, // 评分人（userid）
  personne: 1, // 评分人是否开启 1开 2否
  upper: 0, // 评分上限
  way: 1, // 评分方式 2非手动 1手动
  remark: '', // 备注
  personnel: 2 // 评分人是否开启0否1是
  // dimensionaId: '' // 维度id
};

// 校验权重
const validateWeight = (val: number, cb: any) => {
  if (props.maxWeight - (props?.data?.weight ? props?.data?.weight : 0) === 100)
    cb('权重总和已经达到100，若要继续新增该工具请先调整其他工具的权重!');
};

const rules = reactive({
  type: [
    {
      required: true,
      message: '请选择'
    }
  ],
  project: [
    {
      required: true,
      message: '请输入'
    }
  ],
  standard: [
    {
      required: true,
      message: '请输入'
    }
  ],
  upper: [
    {
      required: true,
      message: '请输入'
    }
  ],
  weight: [
    {
      required: true,
      message: '请输入'
    },
    {
      validator: validateWeight
    }
  ]
});

watch(
  () => props.visible,
  (newVal: boolean) => {
    // 如果是编辑，则赋值给formData
    if (newVal) {
      if (props.data) {
        formData.data = cloneDeep(props.data);
        isShowContinueBtn.value = false;
      } else {
        formData.data = cloneDeep(originData);
      }
      // 如果设置了加权则要设置总分上限
      formData.data.upper =
        toolStore.getCalculate === 2 && toolStore.upperLimit === 1
          ? toolStore.getUpperScore * props.weight * 0.01
          : undefined;
    } else {
      isShowContinueBtn.value = true;
      formData.data = cloneDeep(originData);
    }
  }
);

const handleOk = async () => {
  if (await refForm.value.validate()) {
    return;
  }
  emits('ppiIndexEmit', cloneDeep(formData.data));
  emits('update:visible', false);
};

const continueAdd = async () => {
  if (await refForm.value.validate()) {
    return;
  }
  emits('ppiIndexEmit', cloneDeep(formData.data), true);
  emits('update:visible', true);
  formData.data = cloneDeep(originData);
  formData.data.upper =
    toolStore.getCalculate === 2 && toolStore.upperLimit === 1
      ? toolStore.getUpperScore * props.weight * 0.01
      : undefined;
};

const handleCancel = () => {
  emits('update:visible', false);
};
</script>

<style scoped lang="less">
.icon {
  cursor: pointer;
  font-size: 20px;
}
</style>
